#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

namespace zy
{
	class string
	{
	public:

		string substr(size_t pos, size_t len = npos);
		size_t find(const char ch);
		size_t find(const char* str,size_t pos = 0);

		typedef char* iterator;
		typedef const char* const_iterator;

		iterator begin()
		{
			return _arr;
		}

		const_iterator begin() const
		{
			return _arr;
		}

		iterator end()
		{
			return _arr + _size;
		}

		const_iterator end() const
		{
			return _arr + _size;
		}


		string(const char* arr = "")
		{ 
			_size = strlen(arr);
			_capacity = _size;
			_arr = new char[_capacity + 1];
			strcpy(_arr, arr);

		}

		string(const string& s)
		{
			_arr = new char[s._capacity + 1];
			strcpy(_arr, s._arr);
			_size = s._size;
			_capacity = s._capacity;
		}

		string& operator=(const string& s)
		{
			delete[] _arr;
			_arr = new char [s._capacity+1];
			strcpy(_arr, s.c_str());
			_size = s._size;
			_capacity = s._capacity;
			return *this;
		}


		~string()
		{
			delete[]_arr;
			_arr = nullptr;
			_capacity = _size = 0;
		}

		size_t  size() const
		{
			return _size;
		}

		size_t  capacity() const
		{
			return _capacity;
		}

		const char* c_str() const 
		{
			return _arr;
		}

		char& operator[](size_t pos)
		{
			assert(pos > -1 && pos < _size);
			return _arr[pos];
		}
		const char& operator[](size_t pos) const 
		{
			assert(pos > -1 && pos < _size);
			return _arr[pos];
		}

		void reserve(size_t n);
		void push_back(char ch);
		void append(const char* str);
		string& operator+=(char ch);
		string& operator+=(const char* str);
		void insert(size_t pos, char ch);
		void insert(size_t  pos, const char* str);
		void erase(size_t pos, size_t len = npos);
		void clear();
		


	private:
		char* _arr;
		size_t _size;
		size_t _capacity;
		static const size_t npos = -1;
		//static const size_t npos;
	};

	//const static size_t string::npos = -1;

	bool operator>(const string& s1, const string& s2);

	bool operator==(const string& s1, const string& s2);
	bool operator>=(const string& s1, const string& s2);
	bool operator<(const string& s1, const string& s2);
	bool operator<=(const string& s1, const string& s2);
	bool operator!=(const string& s1, const string& s2);
	istream& getline(istream& is, string& str);
	ostream& operator<<(ostream& out, const string& s);
	istream& operator<<(istream& in, string& s);


}
